package com.ciwong.mobilelib.utils.animtoast;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ciwong.mobilelib.R;


/**
 * Created by keepon on 2018/2/9.
 */

public class ToastNative extends BaseToastAnim {
    private String TAG = "ToastUtil";
    private Context mContext;
    private Toast mToast;//在类前面声明吐司，确保在这个页面只有一个吐司
    private TextView mTextView;
    private CharSequence mToastContent = "";
    private int mDuration = Toast.LENGTH_LONG;
    private static final int TYPE_HIDE_TOAST = 1;//隐藏
    private long calculationDuration = 3000;

    public ToastNative(Context context) {
        this.mContext = context;
        init(context);
    }

    /**
     * @param context 上下文
     */
    public void init(Context context) {
        try {
            mToast = new Toast(context);
        } catch (Exception e) {
            Log.e(TAG, "toast Exception" + e.getMessage());
        }
    }

    /**
     * 销毁toast 防止内存泄露
     *
     * @param toast
     * @param duration
     * @param toastContent 显示的文本
     * @param duration     显示的时长
     */
    private Toast getDefaultToastView(Context context, Toast toast, int duration, CharSequence toastContent) {

        toast.setGravity(Gravity.BOTTOM, 0, context.getResources().getDisplayMetrics().widthPixels / 3);//设置toast显示的位置，这是居中
        ViewGroup inflate = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.toast_anim_integral, null);
        mTextView = inflate.findViewById(R.id.tv_integral_toast);
        if (toastContent != null)
            mTextView.setText(toastContent);
        mTextView.setVisibility(View.INVISIBLE);
        //显示时长处理
        toast.setDuration(Toast.LENGTH_LONG);//默认设置长时间显示
        if (duration == Toast.LENGTH_LONG || duration == Toast.LENGTH_SHORT) {
            toast.setDuration(duration);
        }
        toast.setView(inflate);
        return toast;
    }

    private void addAnimVeiw(Context context, View view, TextView mTextView, long calculationDuration) {
        ToastAnimContal.addAnim(context, view, mTextView, calculationDuration, new IFinishAnimToast() {
            @Override
            public void animFiish(View view) {
                cancelToast(mToast);
                if (iRemoveViewListener != null)
                    iRemoveViewListener.viewRemoved();
            }
        });
    }

    public void show() {
        Toast defaultToastView = getDefaultToastView(mContext, mToast, mDuration, mToastContent);
        if (defaultToastView != null) {
            addAnimVeiw(mContext, defaultToastView.getView(), mTextView, getCalculationDuration());
            defaultToastView.show();
        }
    }

    private void cancelToast(Toast toast) {
        if (toast != null) {
            toast.cancel();
        }
    }

    public CharSequence getmToastContent() {
        return mToastContent;
    }

    public void setmToastContent(CharSequence mToastContent) {
        this.mToastContent = mToastContent;
    }

    public int getmDuration() {
        return mDuration;
    }

    public void setDuration(int mDuration) {
        this.mDuration = mDuration;
    }

    public long getCalculationDuration() {
        calculationDuration = mDuration == Toast.LENGTH_SHORT ? 2000 : 3000;
        return calculationDuration;
    }
}
